#include "at32f403a_407_wk_config.h"
#include "wk_acc.h"
#include "wk_tmr.h"
#include "wk_gpio.h"
#include "wk_system.h"
#include "usbd_core.h"
#include "cdc_msc_class.h"
#include "cdc_msc_desc.h"
#include "usbd_int.h"
#include "ff.h"
#include "operate_spim.h"
#include "st7735.h"
#include "firmwares_burn.h"
#include "app_main.h"
#include "msc_iap_user.h"

// #include "uart_isp.h"
#include "string.h"

usbd_core_type usb_core_dev;

// static const uint32_t version  __attribute__((section(".ARM.__at_"  INT_TO_STR(0x8001F00)))) __attribute__((used)) = 0x76543210;



int main(void)
{
    wk_system_clock_config();
    wk_periph_clock_config();
    wk_nvic_config();
    wk_acc_init();
    wk_timebase_init();
    
    wk_gpio_config();
    
    // wk_usart1_init();
    // wk_uart5_init();
    
    wk_tmr3_init();
    wk_tmr6_init();
    // wk_tmr8_init();
    
    spim_init();
    
    
    if(iap_check_upgrade() == SUCCESS)
    {
        // ST7735_Init();
        if(iap_upgrade_process() == SUCCESS)
        {
        }
    }
    else
    {
        if(iap_get_upgrade_flag() == SUCCESS)
        {
            /* jump to app */
            if(((*(uint32_t*)(FLASH_APP_ADDRESS + 4)) & 0xFF000000) == 0x08000000)
            jump_to_app(FLASH_APP_ADDRESS);
        }
    }
    
    
    crm_usb_clock_source_select(CRM_USB_CLOCK_SOURCE_HICK);
    usbd_core_init(&usb_core_dev, USB, &cdc_msc_class_handler, &cdc_msc_desc_handler, 0);
    usbd_connect(&usb_core_dev);
    
    
    // Board_Init();
    
    log("Hello World\r\n");
    while(1)
    {
        
    }
}

